package com.wwtech.epp.view.dynamicapp;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wwtech.epp.R;
import com.wwtech.epp.entity.AttachmentBean;
import com.wwtech.epp.entity.DynamicUI;
import com.wwtech.epp.ui.adapter.work.AttachmentAdapter;

import java.util.ArrayList;

import io.rong.imkit.activity.FileListActivity;

/**
 * Created by hu on 2017/9/18.
 * 附件
 */

public class AttachementAppView extends LinearLayout{
    private String attachmentUrl = "";
    private DynamicUI.DataBean dataBean;

    AttachmentAdapter attachmentAdapter;

    public String getAttachmentUrl() {
        return attachmentUrl;
    }

    public void setAttachmentUrl(String attachmentUrl) {
        this.attachmentUrl = attachmentUrl;
    }

    public void setNewData(AttachmentBean attachmentBean) {
        this.attachmentUrl = attachmentBean.getUrl();
        if (attachmentAdapter == null) {
            ArrayList<AttachmentBean> list = new ArrayList<>();
            list.add(attachmentBean);
            attachmentAdapter = new AttachmentAdapter(list);
            showAttachment.setAdapter(attachmentAdapter);
        } else {
            attachmentAdapter.addData(attachmentBean);
            attachmentAdapter.notifyDataSetChanged();
        }
    }

    private String key;
    private TextView title;


    public void setTitle(CharSequence title) {
        this.title.setText(title);
    }

    private ImageView addAttachment;
    private RecyclerView showAttachment;
    public AttachementAppView(Context context) {
        super(context);
        init(context);
    }

    public void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.appview_attachment, this, true);
        title = view.findViewById(R.id.tv_attachment);
        addAttachment = view.findViewById(R.id.add_attachment);
        showAttachment = view.findViewById(R.id.attachmentRecyclerView);
        showAttachment.setLayoutManager(new LinearLayoutManager(context));
        addAttachment.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(context, FileListActivity.class);
                intent1.putExtra("rootDirType", 101);
                intent1.putExtra("fileFilterType", 6);
                intent1.putExtra("fileTraverseType", 201);
                intent1.putExtra("rootDir", getSDCardPath());
                ((Activity)context).startActivityForResult(intent1, 730);
            }
        });
    }

    public String getSDCardPath() {
        String SDCardDefaultPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        if(SDCardDefaultPath.endsWith("/")) {
            SDCardDefaultPath = SDCardDefaultPath.substring(0, SDCardDefaultPath.length() - 1);
        }
        return SDCardDefaultPath;
    }

    public DynamicUI.DataBean getDataBean() {
        return dataBean;
    }

    public void setDataBean(DynamicUI.DataBean dataBean) {
        this.dataBean = dataBean;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}
